package client;

import java.util.ArrayList;
import java.util.List;

import client.bean.Member;
import client.bean.Plan;
import client.utils.observer.Observable;
import client.utils.observer.Observer;

public class PlanModel {
	public static final PlanModel instance = new PlanModel();

	private Observable observable = new Observable();
	private List<Plan> plans = new ArrayList<Plan>();

	private PlanModel() {
	}

	public List<Plan> getPlans() {
		return plans;
	}

	public void setPlans(List<Plan> plans) {
		this.plans = plans;
		update();

	}

	public void addMember(Plan plan, Member member) {
		plan.addMember(member);
		update();
	}

	public void addPlan(Plan plan) {
		plans.add(plan);
		update();
	}

	private void update() {
		observable.setChanged();
		observable.notifyObservers();
	}

	public void addObserver(Observer observer) {
		observable.addObserver(observer);
	}

	public int getLatestPlanId() {
		int latestId = 0;
		for (Plan plan : plans) {
			if (plan.getId() > latestId) {
				latestId = plan.getId();
			}
		}
		return latestId;
	}
}
